<!--
 * @Descripttion: 订单管理
 * @version:
 * @Author: 赵致喜
 * @Date: 2020-10-12 18:48:41
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-26 19:07:10
-->
<template>
	<div class="bigDiv">
		<el-form label-position="'right'" label-width="120px" label-suffix=":">
			<div class="topNav">
				<el-button @click="refreshControlHandle" type="text" style="float: right;margin-left:20px;">重置</el-button>
				<el-button @click="searchControlHandle" type="text" style="float: right;">查询</el-button>
			</div>
			<el-col :span="6">
				<el-form-item label="发票抬头">
					<el-select v-model="searchFormData.invoices_id"  clearable filterable>
						<el-option :value="item.invoices_id" :label="item.unit_name" v-for="item in headerArr"></el-option>
					</el-select>
				</el-form-item>
			</el-col>
		</el-form>
		<div class="table-box">
			<el-table :data="tableData" border  @selection-change="handleSelectionChange"
				height="calc(100vh - 290px)">
				<!-- <el-table-column type="selection" width="50" fixed="left"></el-table-column> -->
				<el-table-column label="序号" width="50" fixed="left">
					<template slot-scope="scope">
						{{scope.$index+1}}
					</template>
				</el-table-column>
				<el-table-column prop="orderNo" label="订单号" fixed="left">
				</el-table-column>
				<el-table-column prop="name" label="下单人">
				</el-table-column>
				<el-table-column prop="sjorgname" label="所属部门">
				</el-table-column>
				<el-table-column prop="unitName" label="发票抬头">
				</el-table-column>
				<el-table-column label="操作">
				  <template slot-scope="scope">
					 <el-button size="mini" @click="fenpei(scope.row)">分配项目经理</el-button>
					 <el-button size="mini" @click="changeHeade(scope.row)">修改发票抬头</el-button>
				  </template>
				</el-table-column>
			</el-table>

			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-size="pageData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageData.total"
				style="text-align: right;">
			</el-pagination>

		</div>



		<el-dialog title="批量对账发送" :visible.sync="remarkDialogShow" width="500px">
			<el-input type="textarea" placeholder="请输入备注" v-model="remark" maxlength="1000" show-word-limit></el-input>
			<div slot="footer" class="dialog-footer">
				<el-button @click="remarkDialogShow = false">取 消</el-button>
				<el-button type="primary" @click="submitHandle(remark)">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog title="分配项目经理" :visible.sync="paperDialogShow" width="500px">
			<el-form label-position="'right'" label-width="120px" label-suffix=":">
				<el-form-item label="发票类型">
					<el-select v-model="paperDict.managerName">
						<el-option :value="item.name" :label="item.name" v-for="item in managerList"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitHandle2()">保存</el-button>
			</div>
		</el-dialog>
		
		<el-dialog title="修改发票抬头" :visible.sync="receiptListDialogShow" width="500px">
			<el-form label-position="'right'" label-width="120px" label-suffix=":">
				<el-form-item label="发票抬头">
					<el-select v-model="receiptParam.invoicesId">
						<el-option :value="item.id" :label="item.unitName" v-for="item in receiptList"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitHandle3()">保存</el-button>
			</div>
		</el-dialog>
	</div>

	</div>
</template>

<script>
	import {
		findTOrderManagerList,
		setReceivingStatus,
		findOrgUserList,
		getUser,
		findOrgByMangerList,
		getTreceipt,
		getOrgLazyTree,
		getOrgLazyTree2,
		getOrgChAll,
		getOrgLazyTree3,
		managerReconciliationExport,
		selManageOrder,
		addReconciliationToPurchase,
		
		errOrder,pairOrder,managerList,invoicesList,
		
		selectReceiptList, updateReceipt
	} from '@/api/admin.api'
	import moment from 'moment'
	export default {
		name: 'supplierReconciliation',
		data() {
			return {
				searchFormData: {
					invoices_id:""
				},
				tableData: [],
				// 分页数据
				pageData: {
					// 当前分页
					pageNum: 1,
					// 分页条数
					pageSize: 10,
					// 数据总数
					total: 0,
				},
				// 表格选中数据
				tableSelectData: [],
				remarkDialogShow: false,
				paperDialogShow: false,
				remark: "",
				people: {
					account: "",
					name: "",
					org: "",
					sjorg: ""
				},
				peopleList: [],
				orgList: [],
				paperDict: {},
				treeData: [],
				// 树配置项
				defaultProps: {
					children: 'children',
					label: 'name',
					isLeaf: 'leaf'
				},
				multipleSelection: [],
				managerList:[],
				
				headerArr:[],
				receiptListDialogShow: false,
				receiptList: [],
				receiptParam: {}
			}
		},
		methods: {
			
			invoicesList(){
				invoicesList().then((res) => {
					this.headerArr = res;
				})
			},
			submitHandle2(){
				pairOrder({
					managerName:this.paperDict.managerName,
					orderNo:this.paperDict.orderNo
				}).then(res=>{
					if(res.code == 200){
						this.$message({
							type: 'success',
							message: '分配成功!'
						});
						this.paperDialogShow = false;
						this.getTableData();
					}
				})
			},
			submitHandle3() {
				updateReceipt(this.receiptParam).then(res => {
					this.receiptListDialogShow = false;
					this.$message.success("操作成功")
				})
			},
			fenpei(row){
				managerList().then(res=>{
					this.managerList = res;
					this.paperDict.orderNo = row.orderNo;
					this.paperDialogShow = true;
				})
			},
			clear() {
				var str = this.searchFormData.orgName;
				if (str.length == 0) {
					this.treeData = [];
					this.searchFormData.orgId = '';
					this.searchFormData.orgName = '';
				}
			},
			enterSearch: function(e) {
				if (e.keyCode === 13) {
					console.log(11);
					console.log(e.target.value);

					getOrgLazyTree3({
						id: e.target.value
					}).then((data) => {
						console.log(data);
						this.treeData = data;
						this.$refs.selectReport.blur();
						var that = this;
						setTimeout(function() {
							that.$refs.selectReport.focus();
						}, 300);
					})
				}
			},
			dataFilter(val) {
				console.log(val);
			},
			changeType(e) {
				this.searchFormData.type = e;
			},
			// 左侧树点击
			changeTree(item) {
				var that = this;
				that.searchFormData.orgName = item.name;
				that.searchFormData.orgId = item.id;
				that.searchFormData.allCh = item.allCh;
				that.$refs.selectReport.blur();
			},
			getOrgLazyTree() {
				var that = this;
				findOrgByMangerList().then((data) => {
					console.log(data);
					that.treeData = data;
				})
			},
			// 组织机构节点数据
			loadNode(node, resolve) {
				if (node.level === 0) {
					// findOrgByMangerList().then((res) => {
					// 	res.forEach(item => {
					// 		// 检查是否有叶子节点
					// 		item['leaf'] = false
					// 	})
					// 	if (res.length > 0) {
					// 		this.form = res[0];
					// 	}
					// 	return resolve(res)
					// })
				} else if (node.data.allCh) {
					getOrgLazyTree({
						id: node.data.id,
						type: node.data.type
					}).then((res) => {
						res.forEach(item => {
							// 检查是否有叶子节点
							item['leaf'] = !item.allCh
						})
						return resolve(res)
					})
				} else {
					return resolve([])
				}
			},
			getReturnNode(node, _array, value) {
				let isPass = node && node.label && node.label.indexOf(value) !== -1
				isPass ? _array.push(isPass) : ''
				if (!isPass && node.level != 1 && node.parent) {
					this.getReturnNode(node.parent, _array, value)
				}
			},
			//优化之后的代码 不管有几级都可以适用,不过用了递归
			filterNode(value, data, node) {
				console.log(value, data, node)
				if (!value) {
					return true
				}
				let _array = [] //这里使用数组存储 只是为了存储值。
				this.getReturnNode(node, _array, value)
				let result = false
				_array.forEach(item => {
					result = result || item
				})
				return result
			},
			getPaper(row) {
				var that = this;
				console.log(row);
				getTreceipt({
					orderNo:row.orderNo
				}).then((res2) => {
					console.log(res2);
					that.paperDict = res2.rows[0];
					that.paperDialogShow = true;
				})
			},
			findOrgByMangerList() {
				var that = this;
				findOrgByMangerList().then((res) => {
					console.log(11111);
					console.log(res);
					that.orgList = res;
				})
			},
			changePeople(e) {
				console.log(e);
				var that = this;
				for (var i = 0; i < that.peopleList.length; i++) {
					if (e == that.peopleList[i].userId) {
						that.people.account = that.peopleList[i].account;
					}
				}
			},
			exportOrder() {
				var that = this;
				var type = this.searchFormData.type;
				const newSearch = {
					orderNo: this.searchFormData.orderNo,
					orderName: this.searchFormData.orderName,
					createTime1: this.searchFormData.createTime[0] ? moment(this.searchFormData.createTime[0]).format(
						'YYYY-MM-DD') : '',
					createTime2: this.searchFormData.createTime[1] ? moment(this.searchFormData.createTime[1]).format(
						'YYYY-MM-DD') : '',
					orgId: this.searchFormData.orgId,
					status: this.searchFormData.status,
					eshopNumber: this.searchFormData.eshopNumber,
					goodsName: this.searchFormData.goodsName,
					sku: this.searchFormData.sku,
					name: this.searchFormData.name,
					...this.pageData,
				}
				if (type == false) {
					newSearch.orgId = "";
					// delete newSearch["orgId"];
					newSearch.allCh = this.searchFormData.allCh;
				} else {
					newSearch.allCh = "";
					// delete newSearch["allCh"];
					newSearch.orgId = this.searchFormData.orgId;
				}
				if (!this.searchFormData.orgId) {
					newSearch.allCh = this.searchFormData.temp;
				}
				console.log(newSearch);
				managerReconciliationExport(newSearch).then((res) => {
					let url = window.URL.createObjectURL(new Blob([res], {
						type: "application/vnd.ms-excel"
					}))
					let link = document.createElement('a')
					link.style.display = 'none'
					link.href = url
					link.setAttribute('download', "项目经理对账.xlsx")
					document.body.appendChild(link)
					link.click()
					that.$message({
						type: 'success',
						message: '导出成功!'
					});
				})
			},
			submitHandle(remark) {
				var that = this;
				var arr = this.multipleSelection;
				if (arr.length <= 0) {
					return this.$message({
						type: 'error',
						message: '请选择操作行!'
					});
				}
				var list = [];
				for (var i = 0; i < arr.length; i++) {
					list.push({
						orderNo: arr[i].orderNo,
						remark: remark,
						sku:arr[i].sku
					})
				}
				addReconciliationToPurchase(list).then((res) => {
					console.log(res);
					this.$message({
						type: 'success',
						message: '对账成功!'
					});
					this.getTableData();
					this.remarkDialogShow = false;
				})
			},
			// 检索订单管理列表
			searchControlHandle() {
				// if (this.searchFormData.orgId) {				// 	this.getTableData()				// }else{				// 	this.$message({				// 		type: 'error',				// 		message: '请选择采购组织!'				// 	});				// }
				this.getTableData()
			},
			handleSizeChange(val) {
				this.pageData.pageSize = val
				this.getTableData()
			},
			handleCurrentChange(val) {
				this.pageData.pageNum = val
				this.getTableData()
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
				console.log(val)
			},
			sendHandle() {
				console.log(this.multipleSelection);
				var arr = this.multipleSelection;
				if (arr.length <= 0) {
					return this.$message({
						type: 'error',
						message: '请选择操作行!'
					});
				}
				this.remarkDialogShow = true;
			},
			objectSpanMethod({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				let addIndex = 0;
				if (columnIndex > 1 && columnIndex < 14 || columnIndex>21) {
					if (rowIndex == row.numAll) {
						return {
							rowspan: row.lengthNum,
							colspan: 1,
						};
					} else {
						return {
							rowspan: 0,
							colspan: 0,
						};
					}
				} else {
					return {
						rowspan: 1,
						colspan: 1,
					};
				}
			},
			// 重置搜索条件
			refreshControlHandle() {
				this.searchFormData = {
					invoicesId: ''
				}
				this.tableData = [];
				this.pageData.total = 0;
			}, 
			// 请求订单管理列表
			getTableData() {
				var that = this;
				var type = this.searchFormData.type;
				const newSearch = {
					invoicesId: this.searchFormData.invoices_id,
					...this.pageData,
				}
				console.log(newSearch);
				errOrder(newSearch).then((res) => {
					var tableData = res.rows;
					that.tableData = tableData;
					that.pageData.total = res.total;
				})

			},
			// 修改发票抬头
			changeHeade(row) {
				selectReceiptList(row.orderNo).then(res => {
					this.receiptListDialogShow = true;
					this.receiptParam.orderNo = row.orderNo
					this.receiptParam.invoicesId = row.invoicesId
					this.receiptList = res;
				})
			},
		},
		created() {},
		mounted() {
			// var that = this;
			// window.addEventListener("keyup", this.enterSearch)
			// getOrgChAll().then((res) => {
			// 	that.searchFormData.temp = res.chAll;
			// 	that.getTableData();
			// })
			
			this.invoicesList();
			this.getTableData();
		},
	}
</script>

<style>
	.el-button--text {
		display: none;
	}
</style>
